package com.sos.core.game 
{
	
	import com.sos.core.DataObject;
	import com.sos.core.Stats;

	public class GameCastEffect extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// Property names
		protected static const		JSON_PROPERTY_TARGET:String			= "target";
		
		// Default values
		protected static const      DEFAULT_NAME:String					= "";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _name:String;
		private var _stats:Object;
		private var _target:uint;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameCastEffect(name:String, data:Object = null)
		{
			super();
			// Init data
			_name = name;
			// clears data
			clear();
			// If data is provided, initializes the formation
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get name():String
		{
			return _name;
		}
		
		public function get target():uint
		{
			return _target;
		}
		
		public function set target(value:uint):void
		{
			_target = value;
		}
		
		public function get stats():Object 
		{
			return _stats;
		}
		
		public function set stats(value:Object):void 
		{
			_stats = value;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		
		override protected function validate(data:Object):void
		{
			// Base properties
			if (!data.hasOwnProperty(JSON_PROPERTY_TARGET)) throwLoadMissingPropError(JSON_PROPERTY_TARGET);
		}
		
		override protected function fill(data:Object):void
		{
			
			// Locals
			var stats:Object = {};
			// Target
			_target = data[JSON_PROPERTY_TARGET]; 
			// Loop over generic stats property
			for (var prop:String in data)
			{
				// Avoid JSON_PROPERTY_TARGETS property
				if (prop != JSON_PROPERTY_TARGET)
				{
					// Check if the stats is a base stat
					if (Stats.isBaseStatName(prop))
					{
						stats[prop] = int(data[prop]);
					}
					else
					{
						stats[prop] = data[prop].toString();
					}
				}
			}
			// Apply the stat object
			_stats = stats;
			
			/* OLD VERSION
			// Locals
			var stats:Object = {};
			// Targets parsing
			var targets:Vector.<uint> = new Vector.<uint>();
			var raw_targets:Array = data[JSON_PROPERTY_TARGETS];
			// Loop over targets player instance id
			for (var i:int = 0; i < raw_targets.length; i++)
			{
				targets.push(uint(raw_targets[i]));
			}
			// Clars old targets and popolate with new values
			_targets.splice(0, _targets.length); 
			for (var j:int = 0; j < raw_targets.length; j++)
			{
				_targets.push(raw_targets[j]);
			}
			// Loop over generic stats property
			for (var prop:String in data)
			{
				// Avoid JSON_PROPERTY_TARGETS property
				if (prop != JSON_PROPERTY_TARGETS)
				{
					// Check if the stats is a base stat
					if (Stats.isBaseStatName(prop))
					{
						stats[prop] = int(data[prop]);
					}
					else
					{
						stats[prop] = data[prop].toString();
					}
				}
			}
			// Apply the stat object
			_stats = stats;
			*/
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function getStat(name:String):int
		{
			if (_stats.hasOwnProperty(name))
			{
				return int(_stats[name]);
			}
			throw new Error("Stat " + name + " does not exists!");
		}
		
		public function getProperty(name:String):String
		{
			if (_stats.hasOwnProperty(name))
			{
				return _stats[name].toString();
			}
			return null;
		}
		
		/*
		public function equals(effect:GameCastEffect):Boolean
		{
			return	_name == effect.name		&&
					_target == effect.target	&&
					ObjectUtil.compare(_name, effect) == 0;
		}
		*/
		public function copy(effect:GameCastEffect):void
		{
			_name = effect.name;
			_target = effect.target;
			_stats = effect._stats; // by reference
		}
		
		override public function clear():void
		{
			// Clear data
			_stats = new Object();
			_target = DataObject.INVALID_ID;
		}
		
	}
}